﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
//using mojoPortal.Web.RS.AppCode.Lib;
using mojoPortal.Web.RS.CRM.AppCode.Lib;

namespace mojoPortal.Web.RS.CRM.Controls
{
    public partial class AttachmentControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {

            }
        }

        protected void lnkDeleteAttachment_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            string accountid = hfAccountId.Value;
            int attachmentid = Convert.ToInt32(e.CommandArgument.ToString());
            DeleteAttachment(accountid, attachmentid, userIdentity.RSCompanyId);
            LoadAttachmentsByAccountID(userIdentity.RSCompanyId, accountid);
        }

        private void DeleteAttachment(string accountid, int attachmentid, int companyid)
        {
            string SQL = "DELETE FROM rs_tblAttachment WHERE AccountID=@accountid AND AttachmentId=@attachmentid AND CompanyID=@companyid";
            SqlCommand sqlCmd = new SqlCommand();

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@accountid", SqlDbType.NVarChar, 15, ParameterDirection.Input, accountid);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@attachmentid", SqlDbType.Int, 0, ParameterDirection.Input, attachmentid);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);
            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
        }

        public bool LoadAttachmentsByAccountID(int companyid, string accountid)
        {
            bool bHasValue = false;
            if (companyid > 0 && accountid != null)
            {
                hfAccountId.Value = accountid;
                hfCompanyId.Value = companyid.ToString();
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                String sqlSELECT = "SELECT * FROM rs_tblAttachment WHERE AccountID=@accountid AND CompanyID=@companyid";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@accountid";
                param.Value = accountid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvAttachments.DataSource = ds;
                gvAttachments.DataBind();
                myConnection.Close();

                if (gvAttachments.Rows.Count > 0)
                {
                    bHasValue = true;
                }
            }
            return bHasValue;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (FileUpload1.HasFile)
                try
                {
                    RSUserIdentity userIdentity = GetRSUserIdentity();
                    int companyid = userIdentity.RSCompanyId;
                    string accountid = hfAccountId.Value;

                    string folder = userIdentity.RSAttachmentPath;
                    string fileLocPath = Server.MapPath(folder);
                    string filename_locpath = fileLocPath + accountid + "_" + FileUpload1.FileName.Replace(" ", "_");
                    FileUpload1.SaveAs(filename_locpath);

                    SaveAttachmentDescription(companyid, accountid, folder, FileUpload1.PostedFile.FileName, FileUpload1.PostedFile.ContentLength, FileUpload1.PostedFile.ContentType);
                    LoadAttachmentsByAccountID(companyid, accountid);

                    //Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + "<br>" + FileUpload1.PostedFile.ContentLength + " kb<br>" + "Content type: " + FileUpload1.PostedFile.ContentType;
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                Label1.Text = "You have not specified a file.";
            }
        }

        private void SaveAttachmentDescription(int companyid, string accountid, string folder, string filename, int contentlength, string contenttype)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            SqlCommand sqlCmd = new SqlCommand();

            string Description = txtDescription.Text;
            string FilePath = folder + accountid + "_" + filename.Replace(" ", "_");

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@FileName", SqlDbType.NVarChar, 150, ParameterDirection.Input, filename);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 155, ParameterDirection.Input, Description);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContentLength", SqlDbType.Int, 0, ParameterDirection.Input, contentlength);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ContentType", SqlDbType.NVarChar, 50, ParameterDirection.Input, contenttype);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@FilePath", SqlDbType.NVarChar, 255, ParameterDirection.Input, FilePath);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountID", SqlDbType.NVarChar, 15, ParameterDirection.Input, accountid);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AttachmentId", SqlDbType.Int, 0, ParameterDirection.Input, 0);
            SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveAttachment");
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            //ucModalActionMessageControl.ShowModal("Client Information Successfully Saved!", "", true);
        }
    }
}